package com.cloud.school.app.service.impl;

import com.cloud.school.app.entity.TbGroup;
import com.cloud.school.app.mapstruct.IGroupConvertMapper;
import com.cloud.school.app.model.GroupModel;
import com.cloud.school.app.repository.TbGroupMapper;
import com.cloud.school.app.service.GroupService;
import com.cloud.school.app.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName GroupServiceImpl
 * @Description TODO
 * @Author 36297
 * @Date 2018/12/14 0:54
 * @Version 1.0
 */
@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private TbGroupMapper tbGroupMapper;
    @Autowired
    private IGroupConvertMapper groupConvertMapper;
    @Autowired
    private IdWorker groupIdWorker;

    @Override
    @Transactional
    public boolean save(GroupModel groupModel) {
        TbGroup group = groupConvertMapper.modelToEntity(groupModel);
        group.setId(groupIdWorker.nextId());
        group.setCreateTime(System.currentTimeMillis());
        return tbGroupMapper.insertSelective(group) > 0;
    }

    @Override
    public boolean delete(Long groupId) {
        return tbGroupMapper.deleteByPrimaryKey(groupId) > 0;
    }

    @Override
    public GroupModel selectByPrimaryKey(Long groupId) {
        return groupConvertMapper.entityToModel(tbGroupMapper.selectByPrimaryKey(groupId));
    }

    @Override
    public List<GroupModel> selectByMajorId(Long majorId) {
        TbGroup group = new TbGroup();
        group.setMajorId(majorId);
        return groupConvertMapper.entityToModel(tbGroupMapper.select(group));
    }
}
